let baseUrl = "/index.php";
if (process.env.NODE_ENV == "development") {
  baseUrl = "/api";
}
export default (filePath) => {
  uni.showLoading({
    title: "上传中",
  });
  return new Promise((resolve, reject) => {
    uni.uploadFile({
      url: baseUrl + "/api/common/upload",
      filePath,
      name: "file",
      formData: {
        group: "ugc",
      },
      header: {
        Accept: "text/json",
        token: uni.getStorageSync("user").token,
      },
      success: (uploadFileRes) => {
        let result = JSON.parse(uploadFileRes.data);
        if (result.code === 0) {
          uni.showToast({
            icon: "none",
            title: result.msg,
          });
        } else {
          return resolve(result.data);
        }
      },
      fail: (error) => {
        console.log("上传失败：", error);
        return resolve(false);
      },
      complete: () => {
        uni.hideLoading();
      },
    });
  });
};
